from scrapegraphai.graphs import SmartScraperGraph
graph_config = {
    "llm": {
        "model": "ollama/deepseek-r1:8b",
        "model_tokens": 8192
    },
    "verbose": True,
    "headless": False,
    # 新增防爬配置
    # "proxy": {
    #     "http": "http://用户名:密码@代理IP:端口",
    #     "https": "http://用户名:密码@代理IP:端口"
    # },
    "headers": {
             "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
        "Accept-Language": "zh-CN,zh;q=0.9,en-US;q=0.8",
        "Referer": "https://www.dianping.com/",
        "Cookie": "dper=02026723b069d06a60ed1b9a7e4820adbde980e56981105f3214027597a50e635b827227e9d4b862cefafa3f64b076fea8dc90ab0f686035a8d3000000008f2800004d470b67a21278b5d219a2d024d3a01ec27874cb6286de457f67acb79f7f1fb52221aa07d57dcf0748b709db7e98df1a; dplet=9748f6424d40dd0db4df86d996a863d3; cy=208; cye=foshan;",
        "Sec-CH-UA": "\"Not_A Brand\";v=\"8\", \"Chromium\";v=\"120\", \"Google Chrome\";v=\"120\"",
        "Sec-CH-UA-Platform": "\"Windows\"",
        "Sec-CH-UA-Mobile": "?0"
    },
    "delay": 10 , # 请求间隔秒数
    "browser_args": {
        "viewport": {"width": 1920, "height": 1080},
        "timezone_id": "Asia/Shanghai",
        "locale": "zh-CN",
        "geolocation": {
            "latitude": 23.1291,  # 佛山纬度
            "longitude": 113.2644 # 佛山经度
        },
        "permissions": ["geolocation"],
        "user_agent_metadata": {
            "platform": "Windows",
            "mobile": 0,
            "brands": [
                {"brand": "Not A Brand", "version": "8"},
                {"brand": "Chromium", "version": "120"},
                {"brand": "Google Chrome", "version": "120"}
            ]
        }
    }
}

# Create the SmartScraperGraph instance提取当前页面所有商户信息，包含店名、人均消费、地址和特色标签
smart_scraper_graph = SmartScraperGraph(
    prompt="提取当前页面的文本",
    source="https://www.dianping.com/foshan/ch10",
    config=graph_config
)

# Run the pipeline
result = smart_scraper_graph.run()
print(result)
# import json
# print(json.dumps(result, indent=4))